<?php

use Drupal\shusy_distribution\Entity\ShusyDistribution;

function shusy_distribution_shusy_distribution_entity_form($form, &$form_state, ShusyDistribution $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_distribution') . '/shusy_distribution.forms.inc';
  $form_state[ShusyDistribution::class] = $e;


  $form['name'] = [
    '#type' => 'textfield',
    '#title' => t('姓名'),
    '#size' => 60,
    '#maxlength' => 128,
    '#required' => TRUE,
    '#default_value' => $e->name,
  ];
  $form['sex'] = [
    '#type' => 'select',
    '#title' => t('性别'),
    '#options' => $e->getSexOptions(),
    '#default_value' => $e->sex,
    '#chosen' => TRUE,
  ];
  $form['phone'] = [
    '#type' => 'textfield',
    '#title' => t('联系电话'),
    '#size' => 60,
    '#maxlength' => 128,
    '#required' => TRUE,
    '#default_value' => $e->phone,
  ];
  $form['bank_no'] = [
    '#type' => 'textfield',
    '#title' => t('收款卡号'),
    '#maxlength' => 32,
    '#required' => TRUE,
    '#default_value' => $e->bank_no,
  ];
  $form['bank_name'] = [
    '#type' => 'textfield',
    '#title' => t('开户行'),
    '#maxlength' => 32,
    '#required' => TRUE,
    '#default_value' => $e->bank_name,
  ];
  $form['address'] = [
    '#type' => 'textfield',
    '#title' => t('地址'),
    '#maxlength' => 255,
    '#required' => TRUE,
    '#default_value' => $e->address,
  ];
  $form['reviewer'] = [
    '#type' => 'textfield',
    '#title' => t('审核人'),
    '#maxlength' => 32,
    '#required' => TRUE,
    '#default_value' => $e->reviewer,
  ];
  $form['level_1'] = [
    '#type' => 'textfield',
    '#title' => t('直推一级'),
    '#maxlength' => 32,
    '#required' => TRUE,
    '#default_value' => $e->level_1,
  ];
  $form['level_2'] = [
    '#type' => 'textfield',
    '#title' => t('间推二级'),
    '#maxlength' => 32,
    '#required' => TRUE,
    '#default_value' => $e->level_2,
  ];
  $form['identity_face'] = [
    '#title' => t('身份证正面照'),
    '#type' => 'managed_file',
    '#default_value' => $e->identity_face,
    '#upload_location' => 'public://tmp/',
    '#required' => TRUE,
  ];
  $form['bank_face'] = [
    '#title' => t('银行卡正面'),
    '#type' => 'managed_file',
    '#default_value' => $e->bank_face,
    '#upload_location' => 'public://tmp/',
    '#required' => TRUE,
  ];
  $form['status'] = [
    '#type' => 'select',
    '#title' => t('状态'),
    '#options' => $e->getStatusOptions(),
    '#default_value' => $e->status,
    '#chosen' => TRUE,
  ];

  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  return $form;
}

function shusy_distribution_shusy_distribution_entity_form_submit($form, &$form_state) {
  $e = $form_state[ShusyDistribution::class];
  if ($e instanceof ShusyDistribution) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->save();
  }
}

function shusy_distribution_shusy_distribution_delete_form($form, &$form_state, ShusyDistribution $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_distribution') . '/shusy_distribution.forms.inc';
  $form_state[ShusyDistribution::class] = $e;
  return confirm_form($form,
    t('Are you sure you want to delete %title?', ['%title' => $e->label()]),
    'admin/content/shusy_distribution/' . $e->id,
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function shusy_distribution_shusy_distribution_delete_form_submit($form, &$form_state) {
  $e = $form_state[ShusyDistribution::class];
  if ($e instanceof ShusyDistribution) {
    $e->delete();
  }
}

function shusy_distribution_clear_form($form, &$form_state) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_distribution') . '/shusy_distribution.forms.inc';
  return confirm_form($form,
    t('确认清空所有团长申请数据?'),
    'admin/content/shusy_distribution',
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function shusy_distribution_clear_form_submit($form, &$form_state) {
  db_truncate('shusy_distribution')->execute();
  db_truncate('shusy_distribution_revision')->execute();
  $form_state['redirect'] = 'admin/content/shusy_distribution';
}
